﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}
    <!--begin code mirror -->
    <!--下面两个是使用Code Mirror必须引入的-->
    <link rel="stylesheet" href="/lib/codemirror-5.31.0/lib/codemirror.css" />
    <script src="/lib/codemirror-5.31.0/lib/codemirror.js"></script>
    <!--Java代码高亮必须引入-->
    <script src="/lib/codemirror-5.31.0/clike.js"></script>
    <!--groovy代码高亮-->
    <script src="/lib/codemirror-5.31.0/mode/groovy/groovy.js"></script>
    <!--引入css文件，用以支持主题-->
    <link rel="stylesheet" href="/lib/codemirror-5.31.0/theme/dracula.css" />

    <!--支持代码折叠-->
    <link rel="stylesheet" href="/lib/codemirror-5.31.0/addon/fold/foldgutter.css" />
    <script src="/lib/codemirror-5.31.0/addon/fold/foldcode.js"></script>
    <script src="/lib/codemirror-5.31.0/addon/fold/foldgutter.js"></script>
    <script src="/lib/codemirror-5.31.0/addon/fold/brace-fold.js"></script>
    <script src="/lib/codemirror-5.31.0/addon/fold/comment-fold.js"></script>
    <!--括号匹配-->
    <script src="/lib/codemirror-5.31.0/addon/edit/matchbrackets.js"></script>
    <!--end Code Mirror -->
    <style>
        *, html, body {
            margin: 0px;
            padding: 0px
        }
    </style>


    <div class="layui-card-body"><button type="button" class="layui-btn SaveBtn">保存</button></div>

<!-- begin code -->
<textarea class="form-control" id="code" name="code">@Html.Raw(Model)</textarea>
<!-- end code-->



<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect', 'notice'], function () {
        var $ = layui.jquery;
    //var initValue = "";
        var templateFileName = "@ViewBag.fileName";

    //根据DOM元素的id构造出一个编辑器
    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: "text/groovy",    //实现groovy代码高亮
        mode: "text/x-java", //实现Java代码高亮
        lineNumbers: true,	//显示行号
        theme: "dracula",	//设置主题
        lineWrapping: true,	//代码折叠
        foldGutter: true,
        gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
        matchBrackets: true,	//括号匹配
        //readOnly: true,        //只读
    });
    editor.setSize('auto', '600px');     //设置代码框的长宽

    // 设置初始文本，这个选项也可以在fromTextArea中配置
    //editor.setOption("value", initValue);
    //    // 编辑器按键监听
    //    editor.on("keypress", function() {
    //    // 显示智能提示
    //    editor.showHint(); // 注意，注释了CodeMirror库中show-hint.js第131行的代码（阻止了代码补全，同时提供智能提示）
    //    });

    //    editor.setValue("");    //先代码框的值清空
    //    editor.setValue(obj.scriptCode);    //给代码框赋值

    // editor.setOption("readOnly", true);


        $(".SaveBtn").click(function () {
            var loadIndex = layer.load(2);
            $.post('/api/admin/template/EditTemplateInfo', { name: templateFileName,content:editor.getValue()}, function (res) {
                layer.close(loadIndex);
                if (res.code == 1) {
                    layer.msg(res.message, { icon: 1 });
                } else {
                    layer.msg(res.message, { icon: 2 });
                }
            }, 'json');
        });
    //var value = editor.getValue();
    //console.log(value);
    });
</script>